package org.x.usermod.service.impl;


import ch.qos.logback.core.util.StringUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.x.commonmod.config.BusinessException;
import org.x.commonmod.config.Result;
import org.x.commonmod.enums.LogAsync;
import org.x.usermod.dto.SysMenuCreateDTO;
import org.x.usermod.dto.SysMenuListDTO;
import org.x.usermod.dto.SysMenuUpdateDTO;
import org.x.usermod.entity.SysMenu;
import org.x.usermod.mapper.SysAclMapper;
import org.x.usermod.mapper.SysMenuMapper;
import org.x.usermod.service.ISysMenuService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.x.usermod.vo.SysMenuListVO;

import java.util.Date;

/**
 * 菜单表 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysAclMapper sysAclMapper;

    /**
     * 分页查询
     *
     * @param sysMenuListDTO
     * @return
     */
    @LogAsync(value = "分页查询菜单列表")
    @Override
    public Page<SysMenuListVO> pageList(SysMenuListDTO sysMenuListDTO) {
        //检验SysMenuListDTO是否为空，不为空才构建查询条件
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtil.isNullOrEmpty(sysMenuListDTO.getName())) {
            wrapper.like(SysMenu::getName, sysMenuListDTO.getName());
        }
        if (!StringUtil.isNullOrEmpty(sysMenuListDTO.getIcon())) {
            wrapper.like(SysMenu::getIcon, sysMenuListDTO.getIcon());
        }
        if (!StringUtil.isNullOrEmpty(sysMenuListDTO.getPath())) {
            wrapper.like(SysMenu::getPath, sysMenuListDTO.getPath());
        }
        if (!StringUtil.isNullOrEmpty(sysMenuListDTO.getParentId())) {
            wrapper.like(SysMenu::getParentId, sysMenuListDTO.getParentId());
        }
        if (!StringUtil.isNullOrEmpty(String.valueOf(sysMenuListDTO.getSort()))) {
            wrapper.like(SysMenu::getSort, sysMenuListDTO.getSort());
        }
        if (!StringUtil.isNullOrEmpty(String.valueOf(sysMenuListDTO.getAclId()))) {
            wrapper.like(SysMenu::getAclId, sysMenuListDTO.getAclId());
        }
        if (!StringUtil.isNullOrEmpty(String.valueOf(sysMenuListDTO.getIsDelete()))) {
            wrapper.like(SysMenu::getIsDelete, sysMenuListDTO.getIsDelete());
        }
        Page<SysMenu> page = sysMenuMapper.paginate(
                sysMenuListDTO.getPage(),
                sysMenuListDTO.getSize(),
                wrapper
        );
        //paginate方法返回的是Page<T>，需要转换成Page<SysUserVo>
        Page<SysMenuListVO> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        return pageVo;
    }

    /**
     * 创建新的菜单
     * 关联sys_acl表
     *
     * @param sysMenuCreateDTO
     * @return
     */
    @Override
    public String create(SysMenuCreateDTO sysMenuCreateDTO) {
        //检查新建菜单是否已存在
        SysMenu sysMenu = sysMenuMapper.selectOneByQuery(new QueryWrapper().eq("name", sysMenuCreateDTO.getName()));
        if (sysMenu != null) {
            throw new BusinessException("菜单已存在");
        }
        sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuCreateDTO, sysMenu);
        //添加关联
        sysMenu.setAclId(sysMenuCreateDTO.getAclId());
        sysMenu.setIsDelete(0);
        sysMenu.setCreatedTime(new Date());
        sysMenuMapper.insert(sysMenu);

        return "添加成功";
        }
    /**
     * 逻辑删除菜单
     *
     * @param id
     * @return
     */
    @Override
    public Result removeById(Integer id) {
      //检查菜单是否存在
        SysMenu sysMenu = sysMenuMapper.selectOneById(id);
        if (sysMenu != null) {
            sysMenu.setIsDelete(1);
            //清理权限关联
            sysMenu.setAclId(null);
            sysMenuMapper.insertOrUpdateSelective(sysMenu);
        }
        return Result.ok("删除成功");
    }

    /**
     * 更新菜单
     *
     * @param sysMenuUpdateDTO
     * @return
     */
    @Override
    public String update(SysMenuUpdateDTO sysMenuUpdateDTO) {
        //检查菜单是否存在
        SysMenu sysMenu = sysMenuMapper.selectOneById(sysMenuUpdateDTO.getId());
        if (sysMenu != null) {
            sysMenu.setName(sysMenuUpdateDTO.getName());
            sysMenu.setIcon(sysMenuUpdateDTO.getIcon());
            sysMenu.setPath(sysMenuUpdateDTO.getPath());
            sysMenu.setParentId(sysMenuUpdateDTO.getParentId());
            sysMenu.setSort(sysMenuUpdateDTO.getSort());
            sysMenu.setAclId(sysMenuUpdateDTO.getAclId());
            sysMenuMapper.insertOrUpdateSelective(sysMenu);
            return "更新成功";
        }
        return "更新失败";
    }

}
